import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rcParams

# 设置中文字体（Windows 示例）
rcParams['font.sans-serif'] = ['SimHei']  # 黑体
rcParams['axes.unicode_minus'] = False

def test_initialization_methods():
    input_size = 3
    hidden_size = 4
    num_samples = 1000
    
    methods = {
        '随机大权重': np.random.randn(input_size, hidden_size),
        '小随机权重': np.random.randn(input_size, hidden_size) * 0.01,
        'Xavier初始化': np.random.randn(input_size, hidden_size) * np.sqrt(1.0 / input_size),
        'He初始化': np.random.randn(input_size, hidden_size) * np.sqrt(2.0 / input_size)
    }
    
    fig, axes = plt.subplots(2, 2, figsize=(12, 8))
    axes = axes.flatten()
    
    for idx, (name, weights) in enumerate(methods.items()):

        print(f"{idx}: {name}:")
        print(f"形状: {weights.shape}\n")
        print(f"{weights}")

        # 分析权重分布
        flat_weights = weights.flatten()
        
        axes[idx].hist(flat_weights, bins=50, alpha=0.7)
        axes[idx].set_title(f'{name}\n均值: {np.mean(flat_weights):.3f}, 标准差: {np.std(flat_weights):.3f}')
        axes[idx].set_xlabel('权重值')
        axes[idx].set_ylabel('频次')
    
    plt.tight_layout()
    plt.show()

# 运行测试
test_initialization_methods()